import { IBind } from "xcore/cmpnt";
import { bindCmpnt, IBindCmpnt } from "./bind.cmpnt";
import { getName } from "./bind.name";
import _ from "lodash";
export { getName }

// 定义对象的类型（根据实际需要调整）
interface AttrObject {
    [key: string]: any;
}

// 定义返回的属性类型
interface Attr {
    label: string;
    name: string;
    data: AttrObject;
    cmpnt?: any; // 假设 cmpnt 是 element 类的一个实例或类似的东西
}

// 定义 bindAttrs 函数的类型
const bindProperty = function (obj: AttrObject, conf: IBind): { [key: string]: Attr } {
    let attrs: { [key: string]: Attr } = {};
    if (!obj) return attrs;

    let objAttr: IBindCmpnt = bindCmpnt(obj);

    Object.keys(obj).forEach((key: string) => {
        if (key.startsWith("_")) return;

        const value = obj[key];
        if (
            _.isFunction(value) ||
            _.isPlainObject(value) ||
            _.isNil(value)) { //  //  ||  (_.isArray(value) && (_.isObject(value[0]))
            return;
        }

        let eles = objAttr.getEles(key, conf);
        eles.forEach((ele: any) => { // 这里使用 any 类型，因为不知道 ele 的具体类型
            if (!attrs[key]) {
                attrs[key] = { label: getName(key), name: key, data: obj };
            }
            ele.valueKey = key;
            attrs[key].cmpnt = ele;
        });
    });

    return attrs;
};

export { bindProperty };